package cn.com.cetccst.mdmdemo.utils;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;

import androidx.appcompat.app.AlertDialog;

public class InputDialog extends AlertDialog.Builder {
    private static int lineResId;
    private static int labelResId;
    private static int valueResId;

    public static int getIntValue(String valueStr, int defaultValue) {
        try {
            return Integer.parseInt(valueStr);
        } catch (Exception e) {
        }
        return defaultValue;
    }

    public static void init(int lineResId, int labelResId, int valueResId) {
        InputDialog.lineResId = lineResId;
        InputDialog.labelResId = labelResId;
        InputDialog.valueResId = valueResId;
    }

    public InputDialog(Context context, String[] label, String[] values, String[] defaultValues, boolean isPassword) {
        super(context);
        init(context, label, values, defaultValues, isPassword);
    }

    private void init(Context context, String[] label, String[] values, String[] defaultValues, boolean isPassword) {
        LinearLayout container = new LinearLayout(context);
        container.setOrientation(LinearLayout.VERTICAL);

        container.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        container.setPadding(20, 0, 20, 0);
        for (int i = 0; i < label.length; i++) {
            View lineView;
            //TextView labelView;
            EditText valueEdit;
            if (lineResId == 0) {
                LinearLayout line = new LinearLayout(context);
                lineView = line;
                line.setOrientation(LinearLayout.HORIZONTAL);
                line.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

                //labelView = new TextView(context);
                //line.addView(labelView);
                //labelView.setPadding(0, 0, 10, 0);

                valueEdit = new EditText(context);
                valueEdit.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                if (isPassword) {
                    valueEdit.setInputType(EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD);
                }
                valueEdit.setHint(label[i]);
                if (defaultValues != null && defaultValues.length > i && defaultValues[i] != null) {
                    valueEdit.setText(defaultValues[i]);
                    values[i] = defaultValues[i];
                }
                line.addView(valueEdit);
            } else {
                lineView = View.inflate(context, lineResId, null);
                //labelView = lineView.findViewById(labelResId);
                valueEdit = lineView.findViewById(valueResId);
            }

            container.addView(lineView);

            final int index = i;
            //line.setBackgroundColor(Color.rgb(0x99,0x99,0x99));
            //labelView.setText(label[i]);
            valueEdit.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    values[index] = s.toString();
                }

                @Override
                public void afterTextChanged(Editable s) {
                }
            });
        }
        setView(container);
    }

    public static AlertDialog getUserInput(Activity activity, String dlgTitle, String[] labels, final Callback ok) {
        return getUserInput(activity, dlgTitle, labels, ok, null);
    }

    public static AlertDialog getUserInput(Activity activity, String dlgTitle, String[] labels, final Callback ok, final Callback cancel) {
        return getUserInput(activity, dlgTitle, labels, null, ok, cancel);
    }

    public static AlertDialog getUserInput(Activity activity, String dlgTitle, String[] labels, String[] defaultValues, final Callback ok) {
        return getUserInput(activity, dlgTitle, labels, defaultValues, ok, null);
    }

    public static AlertDialog getUserInput(Activity activity, String dlgTitle, String[] labels, String[] defaultValues, final Callback ok, final Callback cancel) {
        return doGetUserInput(activity, dlgTitle, labels, defaultValues, false, ok, cancel);
    }

    public static AlertDialog getUserInputPassword(Activity activity, String dlgTitle, String[] labels, final Callback ok) {
        return getUserInputPassword(activity, dlgTitle, labels, ok, null);
    }

    public static AlertDialog getUserInputPassword(Activity activity, String dlgTitle, String[] labels, final Callback ok, final Callback cancel) {
        return doGetUserInput(activity, dlgTitle, labels, null, true, ok, cancel);
    }

    public static AlertDialog doGetUserInput(Activity activity, String dlgTitle, String[] labels, String[] defaultValues, boolean isPassword, final Callback ok, final Callback cancel) {
        String[] values = new String[labels.length];

        //InputDialog inputDialog = new InputDialog(activity, R.layout.input_dlg_line_item, R.id.labelView, R.id.valueEdit, labels, values);
        InputDialog inputDialog = new InputDialog(activity, labels, values, defaultValues, isPassword);
        AlertDialog dlg = inputDialog.setTitle(dlgTitle).setCancelable(false).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                ok.handleResult(values);
            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                if (cancel != null) {
                    cancel.handleResult(null);
                }
            }
        }).show();

        dlg.getButton(android.app.AlertDialog.BUTTON_POSITIVE).setTextColor(Color.BLUE);
        dlg.getButton(android.app.AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.BLUE);
        return dlg;
    }

    public interface Callback {
        void handleResult(String[] values);
    }
}
